package com.lyl.momanapp.face.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

import com.lyl.momanapp.util.utlis.MyUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;



public class Utils {
    public static final int MEDIA_TYPE_IMAGE = 1;
    private static final int TYPE_IMAGE = 1;
    private static final String TAG = "Utils";

    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        options.inSampleSize = calculateInSampleSize(options, 480, 800);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    public static String getImageStr(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return MyBase64.encode(data);
    }

    /**
     * 调用系统裁减功能，裁减某张指定的图片，并输出到指定的位置
     *
     * @param activity
     * @param originalFileUri 原始图片位置
     * @param outputFileUri   裁减后图片的输出位置，两个地址最好不一样。如果一样的话，有的手机上面无法保存裁减的结果
     * @return
     */
    public static void cropImage(Activity activity, Uri originalFileUri, Uri outputFileUri, int requestCode, float aspectX, float aspectY, int outputX,
                                 int outputY) {
        if (originalFileUri == null) {
            return;
        }

        final Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(originalFileUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", aspectX);
        intent.putExtra("aspectY", aspectY);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);

        intent.putExtra("scale", true);
        intent.putExtra("scaleUpIfNeeded", true); // 部分机型没有设置该参数截图会有黑边
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        // 不启用人脸识别
        intent.putExtra("noFaceDetection", false);
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * Create a file Uri for saving an image or video
     *
     * @param context
     * @param type    the type of the file you want saved {@link #MEDIA_TYPE_IMAGE}
     * @return return the uri of the file ,if create failed,return null
     */
    public static Uri getOutputMediaFileUri(Context context, int type) {
        File file = getOutputMediaFile(context, type);
        if (file == null) {
            return null;
        }
        return Uri.fromFile(file);
    }

    /**
     * Create a file for saving an image or video,is default in the
     * ../Pictures/[you app PackageName] directory
     *
     * @param context
     * @param type    the type of the file you want saved {@link #MEDIA_TYPE_IMAGE}
     * @return return the file you create,if create failed,return null
     */
    private static File getOutputMediaFile(Context context, int type) {
        String filePath = null;
        if (type == MEDIA_TYPE_IMAGE) {
            filePath = getRandomImageFilePath();
        } else {
            return null;
        }
        if (TextUtils.isEmpty(filePath)) {
            return null;
        } else {
            return new File(filePath);
        }
    }

    public static String getRandomImageFilePath() {
        return getPublicFilePath(TYPE_IMAGE);
    }

    private static String getPublicFilePath(int type) {
        String fileDir = null;
        String fileSuffix = null;
        switch (type) {
            case TYPE_IMAGE:
                fileDir = MyUtils.getInstance().mPicturesDir;
                fileSuffix = ".jpg";
                break;
        }
        if (fileDir == null) {
            return null;
        }
        File file = new File(fileDir);
        if (!file.exists()) {
            if (!file.mkdirs()) {
                return null;
            }
        }
        return fileDir + File.separator + UUID.randomUUID().toString().replaceAll("-", "") + fileSuffix;
    }

    public static float[] getFloatsL3(List<Float> input){
        float[] floats = new float[]{0, 0, 0};
        floats[0] = input.get(0);
        floats[1] = input.get(1);
        floats[2] = input.get(2);

        Log.e(TAG,input.get(0)+"--"+input.get(1)+"--"+input.get(2));

        return floats;
    }

    public static String getTimeStamp(){
        long subTimestamp = System.currentTimeMillis();
        return MD5Utils.MD5Encode(String.valueOf(subTimestamp).substring(0, 10), "utf-8");
    }

    /**
     * 根据当前时间戳生成md5当作token
     * @return token
     */
    public static String genToken(){
        long subTimestamp = System.currentTimeMillis();
        String token = MD5Utils.MD5Encode(String.valueOf(subTimestamp).substring(0, 10), "utf-8");
        return token;
    }

}
